package com.vhall.component.dao.gift;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.vhall.component.entity.gift.dto.GiftBindListDTO;
import com.vhall.component.entity.gift.dto.RoomGiftDTO;
import com.vhall.component.entity.gift.entity.GiftMapping;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface GiftMappingMapper extends BaseMapper<GiftMapping> {
    /**
     * 获取当前房间下关联的表情列表信息
     *
     * @param roomId     房间id
     * @param keyword    表情名称-模糊搜索
     * @param startIndex 分页起始位置
     * @param pageSize   单页大小
     * @return 分页列表
     */
    @Select("<script>" +
            " select gm.id as bind_id,gm.app_id,gm.room_id,gm.gift_id as id,g.name,g.price,gm.created_at,gm.creator_id,gm.sort,g.image_url from gift_mapping gm " +
            " left join gift g on g.id=gm.gift_id " +
            " <where> " +
            " g.deleted=0 and gm.room_id=#{roomId} and g.app_id=#{appId} and gm.app_id=#{appId} " +
            " <if test='keyword!=null'>" +
            " and g.name like concat('%',#{keyword},'%')" +
            "</if>" +
            "</where>" +
            " order by gm.sort limit #{startIndex},#{pageSize}" +
            "</script>")
    List<GiftBindListDTO> selectBindRoomGiftByRoomIdLikeGiftNamePageList(@Param("roomId") String roomId, @Param("appId") String appId, @Param("keyword") String keyword, @Param("startIndex") int startIndex, @Param("pageSize") Integer pageSize);

    @Update("update gift_mapping gm " +
            "set sort=sort+#{changeRule} " +
            "where gm.room_id=#{roomId} " +
            "and sort >=#{startIndex} " +
            "and sort <=#{endIndex}")
    boolean updateRegionGiftMappingSort(@Param("roomId") String roomId, @Param("startIndex") Integer startIndex, @Param("endIndex") Integer endIndex, @Param("changeRule") Integer changeRule);

    /**
     * 获取房间关联的表情数据
     *
     * @param roomId    房间id
     * @param pageIndex 起始位置
     * @param pageSize  单页大小
     * @return 分页数据
     */
    @Select(" SELECT gm.id AS bind_id,gm.gift_id AS id,gm.app_id,gm.sort,gift.NAME,gift.price,gm.creator_id,gift.image_url,gift.created_at " +
            " FROM gift_mapping gm " +
            " LEFT JOIN gift ON gift.id=gm.gift_id " +
            " WHERE gm.room_id=#{roomId} and gm.deleted=0 and gift.app_id=#{appId}" +
            " ORDER BY gm.sort LIMIT #{pageIndex},#{pageSize}")
    List<RoomGiftDTO> roomGiftMappingList(@Param("roomId") String roomId, @Param("appId") String appId, @Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize);
}